package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference//跨服务调用使用  用于RPC功能
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    //手机号唯一性检查
    @Override
    public Boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count > 0;
    }


    //短信验证码发送
    @Override
    public void sendVerifyCode(String phone) {
        //1创建短信验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "")
                .substring(0, 4);
        //2创建一个短信并发送短信
        StringBuilder sb = new StringBuilder(100);
        sb.append("你注册的短信验证码是:").append(code).append(",请在")
                .append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟内使用");

        //假装已经发送短信
        System.out.println(sb.toString());

        //将验证码code缓存到redis中
        userInfoRedisService.addVerifyCode(phone, code);
    }

    //用户注册
    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {
        //1参数是否为空校验
        AssertUtil.hasLength(phone, "手机号是否为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认不能为空");
        AssertUtil.hasLength(verifyCode, "短信验证码不能为空");

        //2.2次密码输入是否正确
        AssertUtil.isEquals(password, rpassword, "两次输入密码必须一致");

        //3手机号码是否是唯一
        if (this.checkPhone(phone)) {//为true就存在

            throw new LogicException("该手机号已被注册了");
        }

        //4短信验证码是否一致
        String code = userInfoRedisService.getVerifyCode(phone);
        if (code == null || !code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("短信验证码错误或失效");
        }
        //5用户注册'
        UserInfo user = new UserInfo();
        user.setNickname(nickname);
        user.setPhone(phone);
        user.setPassword(password);
        user.setHeadImgUrl("/images/default.jpg");
        user.setLevel(1);
        user.setState(UserInfo.STATE_NORMAL);//状态控制

        userInfoMapper.insert(user);
    }

    @Override
    public String userLogin(String username, String password) throws LogicException{
        //1获取参数username和password
         UserInfo user = userInfoMapper.selectUserByUsernameAndPassword(username,password);
         //2判断对象是否存在  不存在提示失败  存在执行下一步要
        if(user == null){
            throw new LogicException("账号或密码错误");
        }
        //3 登录成功后定义一个变量名为token  值为UUID随机值
        String token = UUID.randomUUID().toString().replaceAll("-","");

        //4 将随机生成的token值作为redis中key  登录用户对象作为redis中value值
        //缓存到redis数据库中  设置token有效时间30分钟
        userInfoRedisService.setToken(token,user);
        return token;


    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }


}
